﻿using System;

namespace PrimeFactor
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try { 
                Console.WriteLine("请输入一个正整数n:");
                String s = Console.ReadLine();
                int n = Int32.Parse(s);
                Console.WriteLine("n的素数因子如下:");
                for (int i = 2; i <= n; i++)
                {
                    if (n % i == 0 && (new Program().IfPrime(i)))
                    {
                        Console.WriteLine(i);
                    }
                }
            }
                catch (System.FormatException)
                {
                    Console.WriteLine("输入格式不正确，请重新输入:");
                    continue;
                }
                catch (System.OverflowException)
                {
                    
                }
            }
        }

        public bool IfPrime(int n)//判断n是否质数
        {
            if (n <= 1)
                return false;
            int factor = n;
            for(int i = 2; i <= Math.Sqrt(n);i++)
            {
                if(n%i == 0)
                {
                    factor = i;
                    break;
                }
            }
            if (factor == n)
                return true;
            return false;
        }
    }
}
